package com.haoshui.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.Category;
import com.haoshui.domain.vo.CategoryExportVo;
import com.haoshui.domain.vo.CategoryListVo;
import com.haoshui.domain.vo.PageVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.service.CategoryService;
import com.haoshui.utils.BeanCopyUitls;
import com.haoshui.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    private  CategoryService categoryService;

    /**
     * 查询所有category
     * @return
     */
    @GetMapping("/content/category/listAllCategory")
    public ResponseResult listAllCategory(){
        //查询所有的Category
        List<Category> categories = categoryService.allCategoryList();

        //封装vo
        List<CategoryListVo> categoryListVos = BeanCopyUitls.copyBeanList(categories, CategoryListVo.class);

        //返回响应
        return ResponseResult.okResult(categoryListVos);
    }


    /**
     * 导出分类表，存为excel
     * @param response
     */
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/content/category/export")
    public void export(HttpServletResponse response){
        //查询出category
        List<Category> categoryList = categoryService.allCategoryList();

        //封装成CategoryExportVo
        List<CategoryExportVo> categoryExportVos = BeanCopyUitls.copyBeanList(categoryList, CategoryExportVo.class);
        try {
            //设置response
            WebUtils.setDownLoadHeader("分类.xlsx", response);

            //写入数据
            EasyExcel.write(response.getOutputStream(), CategoryExportVo.class).autoCloseStream(Boolean.FALSE).sheet("模板")
                    .doWrite(categoryExportVos);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            String s = JSON.toJSONString(responseResult);
            WebUtils.renderString(response, s);
        }
    }


    /**
     * 页查询分类，如果有条件就模糊查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/content/category/list")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult pageCategory(Integer pageNum, Integer pageSize, String name, String status){
        //分页查询
        Page<Category> page =  categoryService.pageCategory(pageNum, pageSize, name, status);

        //封装Vo
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        return ResponseResult.okResult(pageVo);

    }


    /**
     * 添加category
     * @param category
     * @return
     */
    @PostMapping("/content/category")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult addCategory(@RequestBody Category category){
        //修改部分值
        category.setPid(-1L);

        //存储
        boolean save = categoryService.save(category);

        if(save){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.INSERT_FAILED);
        }
    }


    /**
     * 修改分类信息，查询步骤
     * @param id
     * @return
     */
    @GetMapping("/content/category/{id}")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult findCategory(@PathVariable("id") Long id){
        //查询分类
        Category category = categoryService.getById(id);

        //返回
        return ResponseResult.okResult(category);
    }


    /**
     * 修改分类信息，更新步骤
     * @param category
     * @return
     */
    @PutMapping("/content/category")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult updateCategory(@RequestBody Category category){
        //更新
        boolean b = categoryService.updateById(category);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }

    }


    //TODO 分类的状态单独修改，前段没有接口，后端暂未实现


    /**
     * 删除分类，逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/content/category/{id}")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult deleteCategory(@PathVariable("id") Long id){
        //删除
        boolean b = categoryService.removeById(id);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
        }
    }

}
